package com.micangcaihang.micang.mvp.main.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import com.afollestad.materialdialogs.MaterialDialog;
import com.micangcaihang.common.base.activity.BaseActivity;
import com.micangcaihang.common.base.fragment.BaseDataBingFragment;
import com.micangcaihang.common.base.fragment.BaseFragment;
import com.micangcaihang.common.utils.AppManager;
import com.micangcaihang.common.widget.titlebar.TitleBar;
import com.micangcaihang.micang.App;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.databinding.ActivityMainFBinding;
import com.micangcaihang.micang.entity.UserInfoSp;
import com.micangcaihang.micang.mvp.contacts.ui.TabContractsFragment;
import com.micangcaihang.micang.mvp.login.ui.LoginActivity;
import com.micangcaihang.micang.mvp.main.contract.DMainContract;
import com.micangcaihang.micang.mvp.main.presenter.DMainPresenter;
import com.micangcaihang.micang.mvp.task.ui.FTabBackTaskFragment;
import com.micangcaihang.micang.mvp.task.ui.FTabToBeTreatedFragment;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/7/30
 *     update : 2017/7/30
 *     desc   : 风控首页
 *     version: 1.0
 * </pre>
 */
public class FMainActivity extends BaseActivity<DMainPresenter, ActivityMainFBinding> implements DMainContract.View {

    private BaseDataBingFragment[] mFragments = new BaseDataBingFragment[3];

    /**
     * 获取资源文件布局
     *
     * @return 资源布局文件layout
     */
    @Override
    protected int getLayoutId() {
        return R.layout.activity_main_f;
    }

    /**
     * 获取传递的bundle数据
     *
     * @param extras
     */
    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected void beforeInitView(Bundle savedInstanceState) {
        super.beforeInitView(savedInstanceState);
        if (savedInstanceState == null) {
            mFragments = new BaseFragment[]{FTabToBeTreatedFragment.newInstance(), FTabBackTaskFragment.newInstance(),
                    TabContractsFragment.newInstance()};
            loadMultipleRootFragment(R.id.main_root, 0, mFragments);
        } else {
            mFragments[0] = findFragment(FTabToBeTreatedFragment.class);
            mFragments[1] = findFragment(FTabBackTaskFragment.class);
            mFragments[2] = findFragment(TabContractsFragment.class);
        }
    }

    /**
     * 初始化view
     */
    @Override
    public void initView() {
        super.initView();
    }

    @Override
    protected void initToolBar() {
        super.initToolBar();
    }

    /**
     * 初始化对象
     */
    @Override
    protected void initData() {
        super.initData();
    }

    /**
     * 简单页面无需mvp就不用管此方法即可,完美兼容各种实际场景的变通
     */
    @Override
    public void initPresenter() {
        super.initPresenter();
    }

    /**
     * 初始化按钮监听
     */
    @Override
    protected void initListener() {
        mViewBinding.btnLogout.setOnClickListener(v -> new MaterialDialog.Builder(FMainActivity.this)
                .content("确定退出当前账户吗?")
                .positiveText("确定")
                .negativeText("取消")
                .onPositive((dialog, which) -> {
                            UserInfoSp.getInstance().clearAccount();
                            App.handler().postDelayed(() -> startActivity(LoginActivity.class), 200);
                            AppManager.getAppManager().finishAllActivity();
                        }
                )
                .show());
        mViewBinding.tab.setOnTabChangedListner(tabNum -> {
            int toDoHidden = -1;
            for (int i = 0; i < mFragments.length; i++) {
                if (!mFragments[i].isHidden()) {
                    toDoHidden = i;
                }
            }
            showHideFragment(mFragments[tabNum], mFragments[toDoHidden]);
//            if (tabNum == 2) {
//                mViewBinding.titleBar.setRightText(null);
//            } else {
//                mViewBinding.titleBar.setRightText(getResources().getString(R.string.common_screening));
//            }
        });
        mViewBinding.titleBar.setDelegate(new TitleBar.Delegate() {
            @Override
            public void onClickLeftCtv() {

            }

            @Override
            public void onClickTitleCtv() {

            }

            @Override
            public void onClickRightCtv() {//筛选

            }

            @Override
            public void onClickRightSecondaryCtv() {

            }
        });
    }

    //点击返回键返回桌面而不是退出程序
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent home = new Intent(Intent.ACTION_MAIN);
            home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            home.addCategory(Intent.CATEGORY_HOME);
            startActivity(home);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
